package com.syq.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.syq.aop.SystemMail;
import com.syq.dto.DTO;
import com.syq.entity.Coach;
import com.syq.entity.Coach;
import com.syq.page.PageInfo;
import com.syq.service.CoachService;
import com.syq.vo.CoachVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin
@Controller
@RequestMapping("/coachController")
public class CoachController {
	@Resource
	CoachService coachService;
	//@RequiresPermissions("coach:getAll")
	@GetMapping("/coachs")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<Coach> getAllCoach() throws Exception {
    	System.out.println("查询全部教练员");
		return coachService.getAllCoachs();
    }
	@RequestMapping("/selectCoachByCno/{cno}")
	@ResponseBody
	public DTO<Coach> selectCoachByCno(@PathVariable("cno") String cno) throws Exception {
		System.out.println("根据主键查询");
		return coachService.selectCoachByCno(cno);
	}

	@RequestMapping("/deleteCoachByCno/{cno}")
	@ResponseBody
    public DTO<Coach> deleteCoachByCno(@PathVariable("cno") String cno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(cno+"删除教练员");
    	return coachService.deleteCoachByCno(cno);
    }

	//@RequestMapping("/updateCoachByCno")
	@PatchMapping("/coachs")
	@ResponseBody
	public DTO<Coach> updateCoachByCno(@RequestBody Coach coach) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改教练员");
		return coachService.updateCoachByCno(coach);
	}
    //@SystemMail(op = "Coach:addCoach",queueName = "family.mail",module = "教练员管理",methods = "教练员注册")
	//@RequestMapping("/addCoach")
	@PostMapping("/coachs")
	@ResponseBody
	public DTO<Coach> addCoach(@RequestBody Coach coach) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加教练员");
 		return coachService.addCoach(coach);
	}

	@RequestMapping("/coachs/page")
	@ResponseBody
	public DTO<Coach> getCoachByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("分页教练员");
		return coachService.getCoachByPage(pageInfo);
	}
	//@RequiresPermissions("coach:selectCoachWithStore")
	@RequestMapping("/selectCoachWithCourseTemplate/{cno}")
	@ResponseBody
	public DTO<Coach> selectCoachWithCourseTemplate(@PathVariable("cno") String cno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询教练员附上门店");
		return coachService.selectCoachWithCourseTemplate(cno);
	}
//	//@RequiresPermissions("coach:getCoachWithOrdrees")
//	@RequestMapping("/getCoachWithOrdrees/{cno}")
//	@ResponseBody
//	public DTO<Coach> getCoachWithOrdrees(@PathVariable("cno") String cno ) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("查询订单");
//		return coachService.getCoachWithOrdrees(cno);
//	}

//	@RequestMapping("/getOrderCookie/{cno}")
//	@ResponseBody
//	public DTO<Coach> getOrderCookie(@PathVariable("cno") String cno, HttpServletRequest request, HttpServletResponse response) throws Exception{
//		//cookie是以key value形式存放在浏览器的文本
//		ObjectMapper objectMapper = new ObjectMapper();
//		DTO dto = new DTO(200, "cookie选课获取成功");
//		Cookie[] cookies = request.getCookies();
//		Coach coach = null;
//		if(Optional.ofNullable(cookies).isPresent()){
//			List<Orderrecord> orderrecords = new ArrayList<Orderrecord>();
//			for(Cookie cookie :cookies){
//				if(cookie.getName().indexOf("coach" + cno+"_")>=0){
//					Orderrecord orderrecord = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), Orderrecord.class);
//					orderrecords.add(orderrecord);
//				}
//				if(cookie.getName().equals("coach" + cno)){
//					coach = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), Coach.class);
//				}
//
//			}
//			if(Optional.ofNullable(coach).isPresent()){
//				coach.setOrderrecords(orderrecords);
//				dto.setT(coach);
//			}else{
//				dto.setCode(404);
//				dto.setMsg("没有cookie");
//			}
//		}
//
//
//		return dto;
//	}

//	public DTO<Coach> saveSelectCourseCookieProcess(Coach coach, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
//		//cookie是以key value形式存放在浏览器的文本
//		DTO dto = new DTO(200,"订单保存成功");
//		if(coach.getCno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			List<Orderrecord> orderrecords = new ArrayList<>(coach.getOrderrecords());
//			for (Orderrecord orderrecord : coach.getOrderrecords()) {
//				Cookie orderrecordsCookie = new Cookie("coach" + coach.getCno() + "_" + orderrecord.getOrrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderrecord), "UTF-8"));
//
//				//coachCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//				orderrecordsCookie.setPath("/");
//				orderrecordsCookie.setDomain("localhost");
//				orderrecordsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//				response.addCookie(orderrecordsCookie);
//
//			}
//			coach.setOrderrecords(null);
//			Cookie coachCookie = new Cookie("coach" + coach.getCno(), URLEncoder.encode(objectMapper.writeValueAsString(coach), "UTF-8"));
//			coachCookie.setPath("/");
//			coachCookie.setDomain("localhost");
//			coachCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(coachCookie);
//			coach.setOrderrecords(orderrecords);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
//		return dto;
//	}

//@RequestMapping("/sendCodeMail/{cname}")
//@ResponseBody
//public DTO<Coach> sendCodeMail(@PathVariable("cname") String cname) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//	System.out.println("找回密码");
//	return coachService.sendCodeMail(cname);
//}
//	@RequestMapping("/updateCoachByPwd")
//	@ResponseBody
//	public DTO<Coach> updateCoachByPwd( CoachVo coachVo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("修改密码"+coachVo);
//		System.out.println(coachVo);
//		return coachService.updateCoachByPwd(coachVo);
//	}
//
//	@RequestMapping("/activate/{cno}")
//	@ResponseBody
//	public DTO<Coach> activate(@PathVariable("cno") String cno) throws Exception{
//
//		return coachService.activate(cno);
//	}

}